package ekahau.positioning.services.providor.server;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.net.Socket;
import java.net.URISyntaxException;
import java.net.URL;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.osgi.framework.Bundle;

import ekahau.positioning.client.ui.ActivatorPlugin;
import ekahau.positioning.services.providor.ServicesClientRequest;
import ekahau.positioning.services.providor.RestaurantsDB;
import ekahau.positioning.services.providor.ServicesClientResponse;
import ekahau.ue.registration.RegistrationClientRequest.PREFERENCE_TYPE;


/**
 * 
 * @author Kashif
 *
 * Unique player connected to the server
 * 
 */
public class EkahauClientListener extends Thread{
	
        private ServicesClientRequest myPosition;
        private Socket socket;
        private ObjectInputStream input;
        private ObjectOutputStream output;
        private ServicesProvidor myGame;

        /**
         * Constructs a handler thread for a given socket and mark
         */
        public EkahauClientListener(Socket socket, ServicesProvidor game) {
            
        	this.socket = socket;
        	myGame = game;
            
            try {
                
            	output = new ObjectOutputStream(socket.getOutputStream());
            	
                
            } catch (IOException e) {
                System.out.println("Player Does not exist:" + e);
            }
        }

        public void run() {
            try {


                // Repeatedly get commands from the client and process them.
                while (true) {
            		input = new ObjectInputStream(socket.getInputStream());
            	    
                    Object object = input.readObject();
                   // Object object = new String();
                    
                    if(object instanceof String){
                    	Bundle bundle = ActivatorPlugin.getDefault().getBundle();
                    	
                    	System.out.println(ActivatorPlugin.getDefault().getBundle().getLocation().substring("reference:file:/".length()));
                    	
                    	System.out.println(ActivatorPlugin.getDefault().getBundle().getLocation()+ "/icons/" + myGame.getPersonLocationSource() + "_" + (String)object + ".jpg");
                    	System.out.println(ActivatorPlugin.getDefault().getBundle().getEntry("/icons/" + myGame.getPersonLocationSource() + "_" + (String)object + ".JPG" ));
                    	
                    	
                    	try {
                    		
                    		
							FileInputStream fis = new FileInputStream("C:\\Documents and Settings\\ssr\\Desktop\\Ekahau_Src_\\EkahauClient\\icons\\A1500_korridoren.jpg" );
                    		//FileInputStream fis = new FileInputStream(ActivatorPlugin.getDefault().getBundle().getLocation().substring("reference:file:/".length()) + "icons/" +  "A1500_korridoren.jpg" );
							byte[] buffer = new byte[fis.available()];
							fis.read(buffer);
							ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()) ;
							oos.writeObject(buffer);
							System.out.println("send");
							//oos.flush() ;
							
							
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
                    }
                    if (object instanceof ServicesClientRequest) {
                    	
                    	ServicesClientRequest pPosition = (ServicesClientRequest)object;  
                    	
                    	if(pPosition.getPreferenceType() == PREFERENCE_TYPE.ALL){
                    		
                    		ServicesClientResponse info = RestaurantsDB.getListByZOne(pPosition.getZoneType());
                    		getOutputSteam().writeObject(info);
                			getOutputSteam().flush();
                    	
                    	}else{
                    		
                    		ServicesClientResponse info = RestaurantsDB.getListType(pPosition.getPreferenceType(), pPosition.getZoneType());
                    		getOutputSteam().writeObject(info);
                			getOutputSteam().flush();
                    	
                    	}
                        	
                    } 
                                      
                    
                }
                
            } catch (IOException e) {
                System.out.println("Client Connection ends: " + e);
                
            }catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
            finally {
                try {
                	
                	myGame.removeClient(this);
                	myGame = null;
                	socket.close();
                } catch (IOException e) {
                	
                }catch(Exception ex){
                	
                }
            }
        }
        
        /**
         * 
         */
        public ServicesClientRequest getPlayerPosition(){
        	
        	return myPosition;
        	
        }
        
        /**
         * 
         */
        public ObjectOutputStream getOutputSteam(){
        	
        	return output;
        	
        }
        
        
        
}


